<?php
	/*
	*	列表控制器
	*	@pageDetail	从数据库获取从 offset 开始的 Limit_value 行数据
	*	@getOffset	从 $_GET 获取到数据库查询偏移量
	*	@display	display 模板文件
	*/
	
	require_once('BaseCtrl.php');

	define('Limit_value', 5);
	
 	
	class OverviewCtrl extends CtrlBase {
		
		private $detail;
		
		private $offset;

		private $model;

		private $pages;
		
		
		public function __construct() {
			
			parent::__construct();
			
			parent::checkSession();

			$this->model = new OverviewModel();
			
		}
		
		
		//	分页查询
		public function pageDetail() {
			
			$this->getOffset();
			
			$detail = $this->model->getDetail(Limit_value, $this->offset);

			if($detail) {
			
				$this->detail = $detail;
				
				return true;
			
			} else {
				
				return false;
				
			}
			
		}


		public function getNumbers() {

		    $num = $this->model->getNumbers();

		    if(!$num) {

		        echo "<script>alert('get numbers error in overview controller')</script>";

		        return false;

            }

		    if(0 === $num%5) {

		        $this->pages = $num / 5;

            } else {

                $this->pages = $num / 5 + 1;

            }

            return true;

        }
		
		
		//	根据GET方式传过来的id获取查分页询的偏移量
		public function getOffset() {
			
			if(isset($_GET['page'])) {
				
				$this->offset = ($_GET['page'] - 1) * 5;
				
			} else {
				
				$this->offset = 0;
				
			}
			
		}
		
		
		public function display() {

		    $this->getNumbers();
			
			if($this->pageDetail()) {

                $this->smarty->assign('numbers', $this->pages);

                $this->smarty->assign('tb', $this->detail);

				$this->smarty->display('list.tpl');
			
			}
			
		}
		
	}
?>